#!/opt/php/bin/php -d display_errors=1
<?php
require_once 'TVB/Message/BaseConsumer.php';
require_once 'TVB/pms/pdes/common/bootstrap.php';
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';

require_once 'TVB/pms/pdes/common/util/db3/Processor.php';
require_once 'TVB/pms/pdes/common/util/db3/PMSDB3Constants.php';
require_once 'TVB/pms/pdes/common/util/db3/GetDataFunctions.php';
require_once 'TVB/pms/pdes/common/util/db3/db3dbhandle.php';
require_once 'TVB/pms/pdes/common/util/db3/InfoData.php';

class ConsumerUnpublished extends BaseConsumer{

	protected function process(Stomp_Frame $message) {
		$type     = (string)$message->map->type;
		$id       = (int)$message->map->id;
		$programme_id = (int)$message->map->programme_id;

		if(isset($message->map->audit_log_id)){
			$audit_id = (int)$message->map->audit_log_id;
		}else{
			$audit_id = 0;
		}

		if(isset($message->map->user)){
			$user = (string)$message->map->user;
		}else{
			$user = null;
		}

		$start_time = microtime(TRUE);
		echo date('Y-m-d H:i:s') . ": Start 'Unpublished'->$type:$id; audit_log_id:$audit_id ...\n";

		$rs = Unpublished::read($type,$id,$audit_id,$user,$programme_id);

		if ($rs === FALSE) {
			// TODO: need some error message
			$rs = $this->sendErrorQueue($message);
		}

		$end_time = microtime(TRUE);
		$time_elapsed = $end_time - $start_time;
		echo date('Y-m-d H:i:s') . ": 'Unpublished' completed. Time used: {$time_elapsed}s\n";

		return $rs;
	}
}

$cons = new ConsumerUnpublished(AC::CONFIG_MQ, 'Unpublished');
try{
	$cons->run();
}catch(Stomp_Exception $e){
	mail(CommonUtils::getAdminMail(),"MQ Server Error, At ".trim(`hostname`) , print_r($e,1));
	echo "MQ Server Error!";
}
